package com.aaa.sys.service.impl;

import com.aaa.sys.entity.User;
import com.aaa.sys.mapper.UserMapper;
import com.aaa.sys.service.MenuService;
import com.aaa.sys.service.RoleService;
import com.aaa.sys.service.UserService;
import com.aaa.sys.util.ActivierUser;
import com.aaa.sys.vo.LoginVO;
import com.aaa.sys.vo.UserPosition;
import com.aaa.sys.vo.UserSearch;
import com.aaa.util.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author ykq
 * @since 2020-11-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuService menuService;

    /**
     * 判断账号或密码是否正确以及该用户具有的权限和角色
     * @param loginVO
     * @return
     */
    @Override
    public ActivierUser queryUserByUserName(LoginVO loginVO) {
        //1.判断账号是否正确
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", loginVO.getUsername());
        User user = this.baseMapper.selectOne(queryWrapper);//根据账号查询用户信息
        if (user == null) {
            return null;
        }
        //2.判断密码是否正确
        String saltMD5 = MD5Util.getSaltMD5(loginVO.getPassword(), user.getSalt());//把输入的密码进行加盐加密
        if (!StringUtils.equals(saltMD5, user.getPassword())) {
            return null;
        }
        //3. 根据用户id查询该用户具有的权限 和角色
        List<String> roles = roleService.queryRolesByUserId(user.getUserId());
        List<String> permissions = menuService.queryPermissionByUserId(user.getUserId());
        ActivierUser activierUser=new ActivierUser(user,roles,permissions);
        return activierUser;
    }

    @Override
    public IPage<UserPosition> selectByPageCon(Integer current, Integer limit, UserSearch userSearch) {
        QueryWrapper<UserPosition> wrapper=new QueryWrapper<>();
        String condition = userSearch.getCondition();
        String startTime=userSearch.getStartTime();
        String endTime = userSearch.getEndTime();

        if(StringUtils.isNotEmpty(startTime)){
            wrapper.ge("a.create_time",startTime.substring(0,10));
        }
        if(StringUtils.isNotEmpty(endTime)){
            wrapper.le("a.create_time",endTime.substring(0,10));
        }

        if(StringUtils.isNotEmpty(condition)){
            wrapper.and(w -> w.like("a.account",condition).or().like("a.name",condition).or().like("a.phone",condition));
        }
        Page<UserPosition> page=new Page<>(current,limit);

        IPage<UserPosition> iPage=this.userMapper.selectByPage(page,wrapper);

        return iPage;
    }

    @Override
    @Transactional
    public void saveUser(UserPosition userPosition) {
            User user=new User();
        BeanUtils.copyProperties(userPosition,user);
        user.setCreateTime(new Date());
        this.save(user);
            //删除该用户原有的职位。
            this.userMapper.deletePos(user.getUserId());
            this.userMapper.savePos(user.getUserId(),userPosition.getPositionId());
    }

    @Override
    public UserPosition getUserPosById(Long userId) {
        return this.userMapper.selectByUserId(userId);
    }

    @Override
    public void updateUser(UserPosition userPosition) {
        User user=new User();
        BeanUtils.copyProperties(userPosition,user);
        user.setUpdateTime(new Date());
        this.updateById(user);
        //删除该用户原有的职位。
        this.userMapper.deletePos(user.getUserId());
        this.userMapper.savePos(user.getUserId(),userPosition.getPositionId());
    }

    @Override
    public void fenUserRole(Long userId, List<Long> roleIds) {
        this.userMapper.deleteRole(userId);
        this.userMapper.saveUserRole(userId,roleIds);
    }
}
